#!/usr/bin/env python
# encoding: utf-8
"""
@author:zwb
@file: views_filters.py
@contact: zwb15083976291@163.com
@time: 2021/11/10
"""
from django.utils.decorators import method_decorator

from admin.serializers.tags import TagSerializer, DeleteTagSerializer, PostTagSerializer, PutTagSerializer, \
    BatchDeleteTagSerializer
from common.models import BlogTag
from utils.filters.views_filters import SearchTagsFilter
from utils.serializers.serializer import PaginatorSerializer
from utils.user_authentication import allow_admin_user
from utils.views import BaseGenericAPIView, BasePaginateGenericAPIView
from utils.responses import CommonResponse, CommonAPIResponseStatus


@method_decorator(allow_admin_user, name='dispatch')
class ManagementTagView(BasePaginateGenericAPIView):
    diff_methods_serializer_class = dict(get=PaginatorSerializer, post=PostTagSerializer,
                                         delete=DeleteTagSerializer, put=PutTagSerializer)
    diff_methods_request_params = dict(
        get=dict(optional=['pageNum', 'pageSize', 'search'], defualt_value=dict(pageNum=1, pageSize=10)),
        post=dict(required=["tag_name"]),
        delete=dict(required=['id']),
        put=dict(required=['id', "tag_name"]))

    filter_backends = [SearchTagsFilter]

    # 查询标签
    def get(self, request):
        ret, message = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=message)
        queryset = BlogTag.objects.order_by('-create_time')
        query_set = self.filter_queryset(queryset=BlogTag.objects.order_by('-create_time'))
        paginator, models = self.paginate_queryset(query_set, self.serializer.validated_data)
        sear_data = dict(instance=models, many=True)
        # 创建文章接口提供
        if request.query_params.get("article", False):
            sear_data["serial_fields"] = ["id", "tag_name"]
        data = self.get_paginated_response(self.serializer.validated_data, paginator,
                                           TagSerializer(**sear_data).data)
        return CommonResponse(data=data)

    # 添加标签
    def post(self, request):
        ret, message = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=message)
        self.serializer.save()
        return CommonResponse(data=self.serializer.data)

    def put(self, request):
        ret, message = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=message)
        self.serializer.select_field_update_obj()
        return CommonResponse(data=self.serializer.data)

    # 删除标签
    def delete(self, request):
        ret, message = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=message)
        # 删除标签
        self.serializer.select_field_delete_obj()
        return CommonResponse(data=self.serializer.data, message="删除成功")


@method_decorator(allow_admin_user, name='dispatch')
class BatchManagementTagView(BaseGenericAPIView):
    serializer_class = BatchDeleteTagSerializer
    generic_request_params = dict(required=['ids'])

    def delete(self, request):
        ret, message = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=message)
        result = BlogTag.objects.filter(id__in=set(self.query_data['ids']))
        data = TagSerializer(serial_fields=["id", "tag_name"],
                             instance=result, many=True).data
        result.delete()
        return CommonResponse(data=data, message="批量删除成功")
